package com.ihg.hiex.admin.service.session.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.ihg.hiex.admin.dao.account.AccountDao;
import com.ihg.hiex.admin.dao.data.ConferenceRoomDao;
import com.ihg.hiex.admin.dao.session.SessionFacilitatorDao;
import com.ihg.hiex.admin.dao.session.SessionMeetingDao;
import com.ihg.hiex.admin.dao.session.SessionMeetingTemplateDao;
import com.ihg.hiex.admin.dao.session.SessionParticipatorDao;
import com.ihg.hiex.admin.service.DataServiceImpl;
import com.ihg.hiex.admin.service.session.SessionMeetingService;
import com.ihg.hiex.entity.data.conference.ConferenceRoom;
import com.ihg.hiex.entity.data.conference.Type;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.session.meeting.SessionMeetingTemplate;
import com.ihg.hiex.entity.session.participator.SessionParticipator;

/**
 *
 * @author Ark
 *
 */
@Service("sessionMeetingService")
public class SessionMeetingServiceImpl extends DataServiceImpl<SessionMeeting> implements SessionMeetingService {

    @Dao
    private SessionMeetingTemplateDao sessionMeetingTemplateDao;
    
    @Dao
    private ConferenceRoomDao conferenceRoomDao;
    
    @Dao
    private SessionParticipatorDao sessionParticipatorDao;
    
    @Dao
    private SessionMeetingDao sessionMeetingDao;
    
    @Dao
    private AccountDao accountDao;
    
    @Dao
    private SessionFacilitatorDao sessionFacilitatorDao;
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionMeetingService#list(com.ihg.hiex.entity.session.Session, java.util.List)
     */
    public List<SessionMeeting> list(Session session, List<String> names) {
        return sessionMeetingDao.list(session, names);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionMeetingService#list(com.ihg.hiex.entity.session.Session, java.lang.String)
     */
    public List<SessionMeeting> list(Session session, String name) {
        return sessionMeetingDao.list(session, name);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionMeetingService#list(java.lang.Long, java.lang.String)
     */
    public List<SessionMeeting> list(Long owner, String name) {
        return sessionMeetingDao.list(owner, name);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionMeetingService#load(com.ihg.hiex.entity.session.Session, java.lang.String)
     */
    public SessionMeeting load(Session session, String name) {
        return sessionMeetingDao.load(session, name);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionMeetingService#load(com.ihg.hiex.entity.session.Session, java.lang.String, java.lang.Long)
     */
    public SessionMeeting load(Session session, String name, Long owner) {
        return sessionMeetingDao.load(session, name, owner);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionMeetingService#load(com.ihg.hiex.entity.session.Session, java.lang.String, java.lang.String)
     */
    public SessionMeeting load(Session session, String name, String subgroup) {
        return sessionMeetingDao.load(session, name, subgroup);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionMeetingService#create(com.ihg.hiex.entity.session.Session)
     */
    public void create(Session session) {
        
        // 1. list not exercise's meeting template
        List<SessionMeetingTemplate> templates = sessionMeetingTemplateDao.list(false);
        Map<String, List<SessionMeetingTemplate>> templateMap = CollectionUtils.constructMapValues(templates, "day");

        // 2. create 1 day session metting
        List<SessionMeetingTemplate> oneDayTemplates = templateMap.get("0");
        List<ConferenceRoom> bigZooms = conferenceRoomDao.list(Type.BigZoom, 10); 
        for (int i = 0; i < oneDayTemplates.size(); i++) {
            ConferenceRoom room = bigZooms.get(i % 2);          // FIXME
            SessionMeeting meeting = new SessionMeeting(session, oneDayTemplates.get(i), room);
            genericDaoSupport.save(meeting);
        }
        
        // 3. create 2 day session meeting
        List<ConferenceRoom> allBigRooms = conferenceRoomDao.list(Type.BigZoom);
        List<SessionMeetingTemplate> twoDayTemplates = templateMap.get("1");
        List<SessionParticipator> sessionParticipators = sessionParticipatorDao.list(session);
        Map<String, List<SessionParticipator>> sessionParticipatorMap = CollectionUtils.constructMapValues(sessionParticipators, "subgroup");
        
        for (int i = 0; i < twoDayTemplates.size(); i++) {
            
            SessionMeetingTemplate template = twoDayTemplates.get(i);
            if (template.isGroup()) {   // FIXME
                int j = 0;
                for(String subgroup : sessionParticipatorMap.keySet()) {
                    ConferenceRoom room = allBigRooms.get(j);
                    j++;
                    SessionMeeting meeting = new SessionMeeting(session, template, room, subgroup);
                    genericDaoSupport.save(meeting);
                }
                
            } else {
            
                ConferenceRoom room = bigZooms.get(0);          // FIXME
                SessionMeeting meeting = new SessionMeeting(session, template, room);
                genericDaoSupport.save(meeting);
                
            }
            
        }
        
        // 4. create 3 day session meeting
        List<SessionMeetingTemplate> threeDayTemplates = templateMap.get("2");
        for (int i = 0; i < threeDayTemplates.size(); i++) {

            SessionMeetingTemplate template = threeDayTemplates.get(i);
            
            if (StringUtils.equals("Development Workshop", template.getName())) {
                ConferenceRoom room = bigZooms.get(0);
                SessionMeeting meeting = new SessionMeeting(session, template, room);
                genericDaoSupport.save(meeting);
            }
            if (StringUtils.equals("Assessor Coaching Skills Recap", template.getName())) {
                ConferenceRoom room = bigZooms.get(1);
                SessionMeeting meeting = new SessionMeeting(session, template, room);
                genericDaoSupport.save(meeting);
            }
            if (StringUtils.equals("Assessor Closing Session", template.getName())) {
                ConferenceRoom room = bigZooms.get(0);
                SessionMeeting meeting = new SessionMeeting(session, template, room);
                genericDaoSupport.save(meeting);
              }
              
              if (StringUtils.equals("Delegate Closing Session", template.getName())) {
                ConferenceRoom room = bigZooms.get(1);
                SessionMeeting meeting = new SessionMeeting(session, template, room);
                genericDaoSupport.save(meeting);
              }
            
        }
        
    }

}
